package cn.tedu.egcar1.controller;

import cn.tedu.egcar1.base.response.JsonResult;
import cn.tedu.egcar1.base.response.StatusCode;
import cn.tedu.egcar1.mapper.UserMapper;
import cn.tedu.egcar1.pojo.dto.UserLoginParam;
import cn.tedu.egcar1.pojo.dto.UserRegParam;
import cn.tedu.egcar1.pojo.entity.User;
import cn.tedu.egcar1.pojo.vo.UserRegQuery;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/v1/user/")
@Slf4j
@Api(tags = "01.用户管理")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @ApiOperation("用户注册")
    @ApiOperationSupport(order = 10)
    @PostMapping("reg")
    public JsonResult reg(@RequestBody UserRegParam userRegParam){
        log.debug("userRegParam"+userRegParam);
        //1、判断用户注册信息是否合法
        //2、判断用户名是否重复（在mapper接口里面创建具体方法，在这里进行调用）
        String username = userRegParam.getUsername();
        UserRegQuery userRegQuery = userMapper.selectByUsername(username);
        if (userRegQuery!=null){//判断用户名是否被占用了
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        //没有被占用就可以进行新用户数据的插入操作了
        User user = new User();
        //复制用户传过来的值
        BeanUtils.copyProperties(userRegParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int i = userMapper.reg(user);
        return JsonResult.ok(user);
    }
    //用户登录
    @PostMapping("login")
    @ApiOperation("登录")
    @ApiOperationSupport(order = 20)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, @ApiIgnore HttpSession session){
        log.debug("userLoginParam="+userLoginParam);
        //先判断用户名和密码是否正确，交给前端判断，这边只输出结果
        String username = userLoginParam.getUsername();
        String password = userLoginParam.getPassword();
        UserRegQuery query = userMapper.selectByUsernameAndPassword(username,password);
        if (query==null){
            return new JsonResult(StatusCode.USER_PASSWORD_ERROR);
        }
        //登录成功的话，就可以进行登录操作了
        session.setAttribute("user",query);
        return JsonResult.ok(query);
    }
    //获取当前用户
    @GetMapping("currentUser")
    @ApiOperation("当前用户")
    @ApiOperationSupport(order = 30)
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        //查看当前用户
        //要看是否已经登录了，如果没登录则去登录，如果登录了，则返回
        UserRegQuery userRegQuery = (UserRegQuery)session.getAttribute("user");
        //交给前端去判断
        //登录成功了则返回
        return JsonResult.ok(userRegQuery);
    }
    //退出登录
    @GetMapping("logout")
    @ApiOperation("退出登录")
    @ApiOperationSupport(order = 40)
    public void logout(@ApiIgnore HttpSession session){
        session.removeAttribute("user");
    }
}
